package cn.caijiapeng.spt.kit;

import javax.servlet.http.HttpServletRequest;

public class RequestKit {
	/** 获取用户IP */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = null;

		ip = request.getHeader("Cdn-Src-Ip");
		if (ip != null && !"".equals(ip)) {
			return ip;
		}

		ip = request.getHeader("x-forwarded-for");
		if (ip != null && ip.indexOf(',') > 0) {
			String[] tmp = ip.split("[,]");
			for (int i = 0; tmp != null && i < tmp.length; i++) {
				if (tmp[i] != null && tmp[i].length() > 0 && !"unknown".equalsIgnoreCase(tmp[i])) {
					ip = tmp[i];
					break;
				}
			}
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if ("0:0:0:0:0:0:0:1".equals(ip)) {
			ip = "127.0.0.1";
		}
		return ip;
	}
	
	/** 获取用户浏览器信息 */
	public static String getUserAgent(HttpServletRequest request) {
		try {
			return request.getHeaders("User-Agent").nextElement();
		} catch (Exception e) {
			return e.getMessage();
		}

	}
	
}